﻿using System;
using MiHoMiao.Mono.UnityPool;
using TMPro;
using TodoList.Mono.MainObject;
using TodoList.TodoTask;
using UI.Dates;
using UnityEngine;

namespace TodoList.Mono.TaskCreator
{
    public class TaskGenerator : MonoBehaviour, IPooledObject
    {
        private static readonly int m_Dead = Animator.StringToHash("Dead");
        [SerializeField] private TMP_InputField m_TaskName;
        [SerializeField] private TMP_InputField m_TaskDescription;
        [SerializeField] private DatePicker m_TaskDeadline;
        [SerializeField] private Animator m_Animator;
        
        public void Ensure()
        {
            Main.Instance.SqlAccess.Insert(
                new TodoTaskTable(
                    m_TaskName.text,
                    m_TaskDescription.text,
                    m_TaskDeadline.SelectedDate
                )
            );
            Main.QueryAll();
            Dead();
        }

        public void ReallyDead()
        {
            UnityPool<TaskGenerator>.Push(this);
        }
        
        public void Dead()
        {
            m_Animator.SetTrigger(m_Dead);
        }
        
        #region IPooledObject
        
        public Component Source { get; set; }
        
        public void OnGet()
        {
            m_TaskDeadline.SelectedDate = DateTime.Now;
            m_TaskName.text = "新建任务";
            m_TaskDescription.text = "全新的任务！尽早完成它吧！";
            Main.Instance.NewButton.SetActive(false);
        }
        
        public void OnRelease()
        {
            transform.localPosition = new Vector3(-400, transform.localPosition.y, 0);
            Main.Instance.NewButton.SetActive(true);
        }
        
        #endregion
    }
}